1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417from kivy.utils import get_color_from_hex as hex_color from kivymd.uix.screen import MDScreen from kivymd.app import MDApp from kivy.lang.builder import Builder from kivymd.uix.label import MDLabel from kivymd.uix.list import MDList from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.relativelayout import MDRelativeLayout from kivymd.uix.selectioncontrol import MDCheckbox from kivymd.uix.button import MDFabButton from kivy.metrics import dp from kivy.utils import get_color_from_hex as hex_colors from kivy.storage.jsonstore import JsonStore from datetime import datetime import uuid from kivy.clock import Clock from kivy.utils import platform from kivy.core.window import Window Window.keyboard_anim_args ={'d':.2, 't':'in_out_expo'} Window.softinput_mode ='below_target' from kivy.uix.scrollview import ScrollView if platform == "android": from android.permissions import request_permissions, Permission from android.storage import app_storage_path, primary_external_storage_path def request_android_permissions(): request_permissions([ Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE ]) class TodoScreen(MDScreen): def on_kv_post(self, base_widget): self.store = JsonStore("todos.json") todos = [] for key in self.store.keys(): data = self.store.get(key) completed = data.get('completed', False) timestamp = data.get('timestamp', "") text = data.get('text', "") todos.append((timestamp, completed, text)) # Sort by timestamp (latest first) todos.sort(reverse=True) for timestamp, completed, todo_text in todos: self.add_list_item(todo_text, completed=completed) def text_length(self, textfield): if len(textfield.text) > 65: textfield.text = textfield.text[:65] from kivy.config import Config from kivy.utils import get_color_from_hex as hex_color # Set window size to mobile dimensions BEFORE importing kivy Config.set('graphics', 'width', '360') Config.set('graphics', 'height', '640') Config.set('graphics', 'resizable', False) from kivymd.uix.screen import MDScreen from kivymd.app import MDApp from kivy.lang.builder import Builder from kivymd.uix.label import MDLabel from kivymd.uix.list import MDList from kivymd.uix.boxlayout import MDBoxLayout from kivymd.uix.relativelayout import MDRelativeLayout from kivymd.uix.selectioncontrol import MDCheckbox from kivymd.uix.button import MDFabButton from kivy.metrics import dp from kivy.utils import get_color_from_hex as hex_colors from kivy.storage.jsonstore import JsonStore from datetime import datetime import uuid from kivy.clock import Clock from kivy.uix.scrollview import ScrollView from kivy.core.window import Window Window.keyboard_anim_args = {'d': .2, 't': 'in_out_expo'} Window.softinput_mode = 'below_target' class TodoScreen(MDScreen): def on_kv_post(self, base_widget): self.store = JsonStore("todos.json") todos = [] for key in self.store.keys(): data = self.store.get(key) completed = data.get('completed', False) timestamp = data.get('timestamp', "") text = data.get('text', "") todos.append((timestamp, completed, text)) # Sort by timestamp (latest first) todos.sort(reverse=True) for timestamp, completed, todo_text in todos: self.add_list_item(todo_text, completed=completed) def text_length(self, textfield): if len(textfield.text) > 65: textfield.text = textfield.text[:65] def toggle_todo(self, instance, text): for key in list(self.store.keys()): if self.store.get(key)['text'] == text: self.store.put(key, text=text, timestamp=self.store.get(key)['timestamp'], completed=instance.active) def delete_todo(self, instance, todo_text): # Find and delete the matching todos based on the text for key in list(self.store.keys()): if self.store.get(key)['text'] == todo_text: self.store.delete(key) break # Now remove the widget from the UI self.ids.todo_list.remove_widget(instance.parent.parent) def add_todo(self): new_text = self.ids.todo_item.text if not new_text.strip(): return # Don't add empty todos key = str(uuid.uuid4()) # Generate a fast unique ID timestamp = datetime.now().isoformat() completed = False # Save to JSON self.store.put(key, text=new_text, timestamp=timestamp, completed=completed) # Add to the top visually self.add_list_item(new_text, insert_top=True) # Clear input self.ids.todo_item.text = "" Clock.schedule_once(lambda dt: setattr(self.ids.scroll_view, 'scroll_y', 1), 0.05) def add_list_item(self, text, insert_top=False, completed=False): box = MDBoxLayout( radius=[dp(15)], orientation="horizontal", size_hint_y=None, size_hint_x=1, height=dp(60), padding=dp(10), spacing=dp(10), pos_hint={"center_x": 0.5, "center_y": 0.5}, md_bg_color=hex_color("#ffffff"), ) layout = MDRelativeLayout(pos_hint={"center_x": 0.5, "center_y": 0.5}) checkbox = MDCheckbox( size_hint=(None, None), size=("48dp", "48dp"), pos_hint={"left": 1, "center_y": 0.5}, color_active=hex_color('#39aacc'), color_inactive=hex_color('#39aacc'), active=completed, on_release=lambda instance: self.toggle_todo(instance, text) ) label = MDLabel( adaptive_size=False, # Disable adaptive_size to prevent font size from changing size_hint_x=1, text_size=(self.width, None), # Adjust text_size considering the left padding padding=(dp(60), dp(5)), # Left padding of 60, no vertical padding halign="left", pos_hint={"center_x": 0.5, "center_y": 0.5}, text=text, font_style="Title", role="small", theme_text_color="Custom", text_color=hex_color('#687179'), bold=True, ) delete_btn = MDFabButton( icon="trash-can", style="small", pos_hint={"right": 1}, theme_text_color="Custom", text_color=hex_color('#ffffff'), theme_bg_color="Custom", md_bg_color=hex_color('#e40f0f'), on_release=lambda instance: self.delete_todo(instance, text) ) layout.add_widget(checkbox) layout.add_widget(label) layout.add_widget(delete_btn) box.add_widget(layout) todo_list = self.ids.todo_list if insert_top: todo_list.add_widget(box) todo_list.children = todo_list.children[::-1] # Insert at top! else: todo_list.add_widget(box) # Normal append at bottom def reload_all_todos(self): self.ids.todo_list.clear_widgets() self.ids.todo_list.height = 0 # Reset the height todos = [] for key in self.store.keys(): data = self.store.get(key) completed = data.get('completed', False) timestamp = data.get('timestamp', "") text = data.get('text', "") todos.append((timestamp, completed, text)) todos.sort(key=lambda x: x[0], reverse=True) for timestamp, completed, todo_text in todos: self.add_list_item(todo_text, completed=completed) from kivy.clock import Clock def search_todos(self, search_widget): search_text = search_widget.text.lower().strip() todo_list = self.ids.todo_list todo_list.clear_widgets() todo_list.height = 0 if not search_text: Clock.schedule_once(lambda dt: self.reload_all_todos(), 0.05) return for key in self.store.keys(): data = self.store.get(key) text = data.get('text', '') completed = data.get('completed', False) if search_text in text.lower(): self.add_list_item(text, completed=completed) # Scroll to top AFTER search items are added Clock.schedule_once(lambda dt: setattr(self.ids.scroll_view, 'scroll_y', 1), 0.05) class MyApp(MDApp): def build(self): kv = Builder.load_file('todo.kv') return kv if __name__ == "__main__": MyApp().run() def toggle_todo(self, instance, text): for key in list(self.store.keys()): if self.store.get(key)['text'] == text: self.store.put(key, text=text, timestamp=self.store.get(key)['timestamp'], completed=instance.active) def delete_todo(self, instance, todo_text): # Find and delete the matching todos based on the text for key in list(self.store.keys()): if self.store.get(key)['text'] == todo_text: self.store.delete(key) break # Now remove the widget from the UI self.ids.todo_list.remove_widget(instance.parent.parent) def add_todo(self): new_text = self.ids.todo_item.text if not new_text.strip(): return # Don't add empty todos key = str(uuid.uuid4()) # Generate a fast unique ID timestamp = datetime.now().isoformat() completed = False # Save to JSON self.store.put(key, text=new_text, timestamp=timestamp, completed=completed) # Add to the top visually self.add_list_item(new_text, insert_top=True) # Clear input self.ids.todo_item.text = "" Clock.schedule_once(lambda dt: setattr(self.ids.scroll_view, 'scroll_y', 1), 0.05) def add_list_item(self, text, insert_top=False, completed=False): box = MDBoxLayout( radius=[dp(15)], orientation="horizontal", size_hint_y=None, size_hint_x=1, height=dp(60), padding=dp(10), spacing=dp(10), pos_hint={"center_x": 0.5, "center_y": 0.5}, md_bg_color=hex_color("#ffffff"), ) layout = MDRelativeLayout(pos_hint={"center_x": 0.5, "center_y": 0.5}) checkbox = MDCheckbox( size_hint=(None, None), size=("48dp", "48dp"), pos_hint={"left": 1, "center_y": 0.5}, color_active=hex_color('#39aacc'), color_inactive=hex_color('#39aacc'), active=completed, on_release=lambda instance: self.toggle_todo(instance, text) ) label = MDLabel( adaptive_size=False, # Disable adaptive_size to prevent font size from changing size_hint_x=1, text_size=(self.width, None), # Adjust text_size considering the left padding padding=(dp(60), dp(5)), # Left padding of 60, no vertical padding halign="left", pos_hint={"center_x": 0.5, "center_y": 0.5}, text=text, font_style="Title", role="small", theme_text_color="Custom", text_color=hex_color('#687179'), bold=True, ) delete_btn = MDFabButton( icon="trash-can", style="small", pos_hint={"right": 1}, theme_text_color="Custom", text_color=hex_color('#ffffff'), theme_bg_color="Custom", md_bg_color=hex_color('#e40f0f'), on_release=lambda instance: self.delete_todo(instance, text) ) layout.add_widget(checkbox) layout.add_widget(label) layout.add_widget(delete_btn) box.add_widget(layout) todo_list = self.ids.todo_list if insert_top: todo_list.add_widget(box) todo_list.children = todo_list.children[::-1] # Insert at top! else: todo_list.add_widget(box) # Normal append at bottom def reload_all_todos(self): self.ids.todo_list.clear_widgets() self.ids.todo_list.height = 0 # Reset the height todos = [] for key in self.store.keys(): data = self.store.get(key) completed = data.get('completed', False) timestamp = data.get('timestamp', "") text = data.get('text', "") todos.append((timestamp, completed, text)) todos.sort(key=lambda x: x[0], reverse=True) for timestamp, completed, todo_text in todos: self.add_list_item(todo_text, completed=completed) from kivy.clock import Clock def search_todos(self, search_widget): search_text = search_widget.text.lower().strip() todo_list = self.ids.todo_list todo_list.clear_widgets() todo_list.height = 0 if not search_text: Clock.schedule_once(lambda dt: self.reload_all_todos(), 0.05) return for key in self.store.keys(): data = self.store.get(key) text = data.get('text', '') completed = data.get('completed', False) if search_text in text.lower(): self.add_list_item(text, completed=completed) # Scroll to top AFTER search items are added Clock.schedule_once(lambda dt: setattr(self.ids.scroll_view, 'scroll_y', 1), 0.05) class MyApp(MDApp): def build(self): kv = Builder.load_file('todo.kv') return kv def on_start(self): if platform == "android": request_android_permissions() if __name__ == "__main__": MyApp().run()